package com.fuhe.chen.vendingmachine.service.impl;

import com.fuhe.chen.vendingmachine.common.redis.RedisUtils;
import com.fuhe.chen.vendingmachine.common.Utils;
import com.fuhe.chen.vendingmachine.dao.CommodityDao;
import com.fuhe.chen.vendingmachine.dao.CommoditySoldDao;
import com.fuhe.chen.vendingmachine.dao.MachineDao;
import com.fuhe.chen.vendingmachine.dao.OrderDao;
import com.fuhe.chen.vendingmachine.dto.CommoditySalesVolumeData;
import com.fuhe.chen.vendingmachine.dto.OrdersMonthVolume;
import com.fuhe.chen.vendingmachine.service.IStatisticService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import static com.fuhe.chen.vendingmachine.constant.CacheConstant.Statistic.*;

@Service
public class StatisticServiceImpl implements IStatisticService {

    @Autowired
    RedisUtils redisUtils;

    @Autowired
    CommoditySoldDao commoditySoldDao;

    @Autowired
    CommodityDao commodityDao;

    @Autowired
    OrderDao orderDao;

    @Autowired
    MachineDao machineDao;

    @Override
    public Map<String, String> OrderData() {
        Map<String, String> data = new HashMap<>();

            String totalSalesAmount = String.valueOf(orderDao.totalSalesAmount());
            String totalSalesOrders = String.valueOf(orderDao.totalSalesOrders());
            String totalOrders = String.valueOf(orderDao.totalOrders());


            data.put(TOTAL_ORDERS,totalOrders);
            data.put(TOTAL_SALES_ORDERS,totalSalesOrders);
            data.put(TOTAL_SALES_AMOUNT,totalSalesAmount);
        return data;
    }

    @Override
    public Map<String, String> CommodityData() {
        Map<String, String> data = new HashMap<>();

        String totalSalesCommodities = String.valueOf(commoditySoldDao.totalSalesCommodities());
        String totalCommodities = String.valueOf(commodityDao.totalCommodities());
        String totalMachines = String.valueOf(machineDao.totalMachines());

        data.put(TOTAL_COMMODITIES,totalCommodities);
        data.put(TOTAL_SALES_COMMODITIES,totalSalesCommodities);
        data.put(TOTAL_MACHINES,totalMachines);
        return data;
    }

    @Override
    public List<CommoditySalesVolumeData> CommoditySalesVolumeData() {
        List<CommoditySalesVolumeData> commoditySalesVolumeData= commoditySoldDao.getCommoditySalesVolumeData();
        return commoditySalesVolumeData;
    }

    @Override
    public List<OrdersMonthVolume> OrdersMonthVolume() {
        List<OrdersMonthVolume> volumes = new ArrayList<>();
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH,1);
        Long firstDay = cal.getTimeInMillis();
        Long lastDay = firstDay + 2592000000L ;

        for(int i = 0; i<6;i++){
            OrdersMonthVolume volume = orderDao.getOrdersMonthVolumes(1,  firstDay, lastDay);

            String month = Utils.fromLongToDate("MM", firstDay);
            volume.setMonth(Utils.getMonthByString(month));

            volumes.add(0,volume);

            lastDay = firstDay;
            firstDay = firstDay - 2592000000L;

        }

        return volumes;
    }

    public Map<String,Integer> MachineStatusData(){

        Map<String, Integer> data = new HashMap<>();
        Integer totalMachines = machineDao.totalMachines();
        Integer totalMachinesInOperation = machineDao.totalMachinesInOperation();
        Integer totalMachinesInError = totalMachines - totalMachinesInOperation;

        data.put(TOTAL_MACHINES_IN_OPERATION,totalMachinesInOperation);
        data.put(TOTAL_MACHINES_IN_ERROR,totalMachinesInError);

        return data;
    }
}
